// packageK/category/category.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    subItems: [],
    parentId: 0,
    mainActiveIndex: 0,
    // 请求参数对象
    queryObj:{
      page: 1,
      page_size: 10
    },
    // 总数量，用来实现分页
    total: 0,
    // 是否正在请求数据
    isloading: false,
    finished: false,
    refreshing: false
  },

  /**
   * 搜索框获取焦点跳转到搜索页面
   */
  toSearch: function() {
    wx.navigateTo({
      url: '/packageI/searchPage/searchPage'
    })
  },

  /**
   * 获取分类
   */
  getClassify: function() {
    const urlStr = app.getNetAddresss('goods.classify.home');
    const that = this;
    this.setData({ items: [{id: 0, text: '推荐品牌'}], subItems: [] });
    app._getNetWork({
      url: urlStr,
      success: function(res) {
        const { result, data, msg } = res.data
        if (result == 1) {
          if (data.categoryList && data.categoryList.data && data.categoryList.data.length) {
            const list = data.categoryList.data.map(item => {
              return {
                id: item.id,
                text: item.name
              }
            })
            that.setData({ 
              items: [...that.data.items, ...list]
            })
            that.getSubClassify()
          }
        } else {
          app.tips(msg || '获取分类数据失败')
        }
      }
    })
  },

  /**
   * 获取子类
   */
  getSubClassify: function() {
    const { queryObj, isloading, subItems, parentId } = this.data
    if (isloading) {
      return
    }
    this.setData({ isloading: true })
    const { page } = queryObj;
    const urlStr = parentId ? app.getNetAddresss('goods.category.get-children-category') : app.getNetAddresss('goods.brand.get-brand');
    const json = { parent_id: this.data.parentId, page };
    const that = this;
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function(res) {
        const { result, data, msg } = res.data
        if (result == 1) {
          that.setData({ subItems: [...subItems, ...data.data]})
          that.setData({
            finished: that.data.subItems.length >= data.total
          })
        } else {
          app.tips(msg || '获取下级分类数据失败')
        }
      },
      complete: function() {
        that.setData({ refreshing: false, isloading: false })
      }
    })
  },

  /**
   * 分类点击获取子类
   * @param {Object} e 
   */
  onClickNav: function(e) {
    const { index } = e.detail
    const { items, parentId } = this.data
    const { id } = items[index]
    if (id != parentId) {
      this.setData({ 
        parentId: id,
        subItems: [],
        refreshing: false
      })
      this.getSubClassify()
    }
  },

  /**
   * 重置页面请求数据
   */
  resetQuery: function() {
    this.setData({
      'queryObj.page': 1,
      total : 0,
      isloading : false,
      subItems : [],
      finished: false
    })
  },

  /**
   * 子类上拉加载更多
   */
  getMoreSubItems: function() {
    if (this.data.finished) {
      return
    }
    this.data.queryObj.page++;
    this.getSubClassify()
  },

  /**
   * 子类下拉刷新
   */
  refreshSubItems: function() {
    if(this.data.isloading) {
      return
    }
    this.resetQuery()
    this.setData({
      refreshing: true
    })
    this.getSubClassify()
  },

  /**
   * 子类点击跳转
   * @param {Object} e 
   */
  gotoCateList: function(e) {
    console.log(e)
    const { id } = e.currentTarget.dataset
    const { parentId } = this.data
    let url = parentId ? `/packageB/member/category/search_v2/search_v2?id=${id}` : `/packageB/member/category/brandgoods/brandgoods?id=${id}`
    wx.navigateTo({ url })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getClassify()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})